package org.jvnet.hudson.client_demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class GenCraTestData {

	public static void main(String[] args) {
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String strSQL = "";
		final String creatorId = "842297";
		final String subsysId = "BL";
		final int craDetSize = (int) (Math.random()*4+1);
		
		boolean[] arrR = new boolean[3];
		boolean[] arrP = new boolean[3];
		Arrays.fill(arrR, Boolean.TRUE);
		Arrays.fill(arrP, Boolean.TRUE);
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			// 註冊driver
			conn = DriverManager.getConnection("jdbc:mysql://10.144.136.54:3306/sysflow", "Arthur","abcd1234");
			conn.setAutoCommit(false);
			
			
			/****建立LOG，第一步就開始記錄程式的執行****/
			strSQL="INSERT INTO changelog (changeLogId,changeTypeId,creatorId,bDate) VALUES (null,1,?,?)";
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, creatorId);
			ps.setTimestamp(2, cDT());
			ps.execute();
			strSQL="SELECT MAX(changeLogId) FROM changelog";
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			rs.next();
			final int updateMod = rs.getInt(1);
			
			/****創建變更申請****/
			strSQL = "INSERT INTO cramain (craId,subsysId,craStatusId,craTypeId,creatorId,cDT) VALUES (null,?,?,?,?,?)";
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, subsysId);
			ps.setInt(2, 5);
			ps.setInt(3, 3);
			ps.setString(4, creatorId);
			ps.setTimestamp(5, cDT());
			ps.execute();
			
			//保存新產生的變更申請序號，等等創建細項要用
			strSQL="SELECT MAX(craId) FROM cramain";
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			rs.next();
			final int craId = rs.getInt(1);		//變更申請序號ID
			
			
			for (int i=1; i<=craDetSize; i++) {
				/****創建變更申請細項****/
				strSQL = "INSERT INTO cradetail (craId,craDetId,modUnitId) VALUES (?,?,?)";
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, craId);
				ps.setInt(2, i);
				ps.setString(3, gMit(i));
				ps.execute();
				
				int ranSize = (int)(Math.random()*3+1);
				
				/****變更申依據需求****/
				strSQL="INSERT INTO mpcrareq (craId,craDetId,reqId) VALUES (?,?,?)";
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, craId);
				ps.setInt(2, i); 	
				for(int j=0; j<ranSize; j++) {
					int jj = (int)(Math.random()*14+1);
					ps.setInt(3, jj);
					ps.execute();
					
					System.out.println("87==>"+craId+"\t"+ i+"\t"+ jj );
				}
				
				int pSize = (int) (Math.random()*3+1);
				
				/****變更申請可操作人員****/
				strSQL="INSERT INTO mpcraperformers (craId,craDetId,performerId,isRole) VALUES (?,?,?,?)";
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, craId);
				ps.setInt(2, i); 	//目前新增只會有一筆的變更申請細項
				for(int k=0; k<pSize; k++) {
					if ( (int)(Math.random()*2)%2 ==1 ) {
						int roleId = (int) (Math.random()*4+3);
						roleId = (roleId==6)?3:roleId;
						if ( arrR[roleId-3] ) {
							ps.setString(3, roleId+"");
							ps.setString(4, "t");
							arrR[roleId-3]=false;
							ps.execute();
						}
					} else {
						int pId = (int)(Math.random()*3+1);
						if ( arrP[pId-1] ) {
							ps.setString(3, gP( pId ));
							ps.setString(4, "f");
							arrP[pId-1]=false;
							ps.execute();
						}
					}
					
				}
			}
			
			/****完成這次的異動****/
			strSQL = "UPDATE changelog SET eDate=? WHERE changeLogId=?";
			ps = conn.prepareStatement(strSQL);
			ps.setTimestamp(1, cDT());
			ps.setInt(2, updateMod);
			ps.execute();
			
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("好了");

	}
	
	private static Timestamp cDT() {
	    return Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	}
	
	private static String gMit(int a) {
		String res="";
		
		if (a==1){
			res = "MOrderSource";
		} else if (a==2){
			res = "MOrderWAR";
		} else if (a==3){
			res = "MOrderEJB";
		} else if (a==4){
			res = "MOrderEAR";
		}
		return res;
	}
	
	private static String gP(int a) {
		String res="";
		
		if (a==1){
			res = "16458";
		} else if (a==2){
			res = "842297";
		} else if (a==3){
			res = "16459";
		}
		return res;
	}

}
